package com.xfc.web.controller.home;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.utils.DateUtils;
import com.xfc.school.domain.ScAuditionClassInfo;
import com.xfc.school.domain.ScCourse;
import com.xfc.school.domain.ScTeacher;
import com.xfc.school.service.IScAuditionClassInfoService;
import com.xfc.school.service.IScCourseService;
import com.xfc.school.service.IScStudentService;
import com.xfc.school.service.IScTeacherService;
import com.xfc.wx.domain.ScWxUser;
import com.xfc.wx.service.IScWxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@Api(value = "home", tags = "首页数据接口")
@RequestMapping("/home")
@RestController
public class HomeController {

    @Autowired
    private IScStudentService studentService;

    @Autowired
    private IScTeacherService teacherService;

    @Autowired
    private IScCourseService courseService;

    @Autowired
    private IScWxUserService wxUserService;

    @Autowired
    private IScAuditionClassInfoService scAuditionClassInfoService;

    /**
     * 获取学生人数
     * @return
     */
    @ApiOperation("获取学生人数")
    @GetMapping("/getStudentNumber")
    public AjaxResult getStudentNumber(){
        Integer count=studentService.count();
        return AjaxResult.success(count);
    }

    /**
     * 获取老师人数
     * @return
     */
    @ApiOperation("获取老师人数")
    @GetMapping("/getTeacherNumber")
    public AjaxResult getTeacherNumber(){
        QueryWrapper<ScTeacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("state",0);
        Integer count=teacherService.count(queryWrapper);
        return AjaxResult.success(count);
    }

    /**
     * 获取课程数量
     * @return
     */
    @ApiOperation("获取课程数量")
    @GetMapping("/getCourseNumber")
    public AjaxResult getCourseNumber(){
        QueryWrapper<ScCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("state",0);
        Integer count=courseService.count(queryWrapper);
        return AjaxResult.success(count);
    }

    /**
     * 获取注册人数
     * @return
     */
    @ApiOperation("获取注册人数")
    @GetMapping("/getRegisterNumber")
    public AjaxResult getRegisterNumber(){
        QueryWrapper<ScWxUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("employee",0);
        queryWrapper.isNotNull("mobile");
        Integer count=wxUserService.count(queryWrapper);
        return AjaxResult.success(count);
    }


    /**
     * 统计试听课（折线图）
     */
    @ApiOperation("统计试听课（折线图）")
    @GetMapping("/getStudentNumberChart")
    public AjaxResult getStudentNumberChart(){
        //获取七天前日期
        List<String> listDate= DateUtils.getAgoDay(15);
        Collections.reverse(listDate);
        Map<String,Object> map=new HashMap<>(1);
        List<Integer> list=new ArrayList<>();
        QueryWrapper<ScAuditionClassInfo> queryWrapper=new QueryWrapper<>();
        listDate.forEach(l->{
            queryWrapper.apply(StringUtils.isNotBlank(l),
                    "date_format (create_time,'%Y-%m-%d') = date_format('" + l + "','%Y-%m-%d')");
            list.add(scAuditionClassInfoService.count(queryWrapper));
        });
        map.put("name","试听课人数");
        map.put("actualData",list);
        map.put("date",listDate);
        return AjaxResult.success(map);
    }


    /**
     * 统计注册用户（折线图）
     */
    @ApiOperation("统计注册用户（折线图）")
    @GetMapping("/getRegisterNumberChart")
    public AjaxResult getRegisterNumberChart(){
        //获取七天前日期
        List<String> listDate= DateUtils.getAgoDay(15);
        Collections.reverse(listDate);
        Map<String,Object> map=new HashMap<>(1);
        List<Integer> list=new ArrayList<>();
        QueryWrapper<ScWxUser> queryWrapper=new QueryWrapper<>();
        listDate.forEach(l->{
            queryWrapper.apply(StringUtils.isNotBlank(l),
                    "date_format (create_time,'%Y-%m-%d') = date_format('" + l + "','%Y-%m-%d')");
            list.add(wxUserService.count(queryWrapper));
        });
        map.put("name","注册人数");
        map.put("actualData",list);
        map.put("date",listDate);
        return AjaxResult.success(map);
    }
}
